<?php
session_start();

// 如果session中不存在 username 就表示登录
if(!isset($_SESSION['username'])){
	// header("location: login.php");
	// die("未登录<script>setTimeout(function(){window.location.href='login.php'},3000)</script>");}
	die("<script>alert('未登录'); window.location.href='../login.php';</script>");
	
}

include("../conn.php");


if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	$title = $_POST['title'];
	$content = $_POST['content'];
	
	$sql = "insert into articles(title, content) values(:title,:content)";
	
	// 连接数据库
	$pdo = getConn();
	
	// 预编译sql语句
	$stmt = $pdo->prepare($sql);
	
	// 绑定参数
	$stmt->bindParam(":title", $title);
	$stmt->bindParam(":content", $content);
	
	// 执行sql语句
	$stmt->execute();
	
	// 判断是否成功
	if($stmt->rowCount()>0) {
		header("Location:list.php");
		exit();
	} else {
		die("添加失败");
	}
}

?>
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<form action="" method="post">
		<div>文章标题：</div>
		<div><input type="text" name="title"></div>
		<div>文章内容：</div>
		<textarea name="content" hidden="hidden" id="content" cols="30" rows="10"></textarea>
		<div id="editor"></div>
		<input type="submit" value="添加文章">
	</form>
	<script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script>
		const editor = new window.wangEditor("#editor");
		
		// 获取textarea元素
		const content = $('#content')
		
		// 第二步，监控变化，同步更新到 textarea
		editor.config.onchange = function (html) {
			content.val(html)
		}
		editor.create();
	</script>
</body>
</html>


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.88.1">
    <title>dise</title>

    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap-grid.rtl.min.css" rel="stylesheet">
    <!-- Bootstrap core CSS -->
	<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet">


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
    <!-- Custom styles for this template -->
    <link href="./css/dashboard.css" rel="stylesheet">
  </head>
  <body>
    
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
  <a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="#">Company name</a>
  <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
  <div class="navbar-nav">
    <div class="nav-item text-nowrap">
      <a class="nav-link px-3" href="#">Sign out</a>
    </div>
  </div>
</header>

<div class="container-fluid">
  <div class="row">
    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
      <div class="position-sticky pt-3">
        <ul class="nav flex-column">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">
              <span data-feather="home"></span>
              Dashboard
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">
              <span data-feather="file"></span>
              Orders
            </a>
          </li>
        </ul>
      </div>
    </nav>

    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">博客</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
        </div>
      </div>
</div>
</div>
<table class="table">
  <thead>
    <tr>
      <th scope="col">编号</th>
      <th scope="col">文章标题</th>
      <th scope="col">发布日期</th>
	  <th scope="col">操作</th>
	  </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
   <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js" integrity="sha384-uO3SXW5IuS1ZpFPKugNNWqTZRRglnUJK6UAZ/gxOX80nxEkN9NcGZTftn6RzhGWE" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js" integrity="sha384-zNy6FEbO50N+Cg5wap8IKA4M/ZnLJgzc6w2NqACZaK0u0FXfOWRRJOnQtpZun8ha" crossorigin="anonymous"></script><script src="./js/dashboard.js"></script>
  </body>
</html>
